package br.unifor.cct.mia.evaluate.classification;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import weka.core.Instances;
import weka.gui.explorer.ClassifierPanel;

public abstract class WekaClassifier {

	public ClassifierPanel panel;
	
	private String options[] = null;	
	//private int testType = ClassifierPanel.CROSS_VALIDATION;
	Integer classifType;
	
	public WekaClassifier(Integer classifType, String[] options) {
		this.options = options;
		this.classifType = classifType;
	}
	
	/*public void setTestType(int type) {
		switch (type) {
			case ClassifierPanel.CROSS_VALIDATION:
				this.testType = ClassifierPanel.CROSS_VALIDATION;
			break;
			case ClassifierPanel.SUPPLIED_TEST_SET:
				this.testType = ClassifierPanel.SUPPLIED_TEST_SET;
			break;
			default:
				this.testType = ClassifierPanel.CROSS_VALIDATION;
			break;
		}
	}*/
	
	public double evaluate(File file) throws FileNotFoundException, IOException, InterruptedException {
		double result = 0;
		panel.setTestType(ClassifierPanel.CROSS_VALIDATION);
		
		try {
			Instances i = new Instances(new BufferedReader(new FileReader(file)));
			panel.setInstances(i);
			panel.startClassifier();
			panel.m_RunThread.join();
			
			result = panel.evaluate;	
			
			panel.m_OutText.setText("");
			i.delete();
			i = null;				
			panel.m_RunThread = null;
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return result;
	}
	
	public double evaluate(File file, File testFile) throws FileNotFoundException, IOException, InterruptedException {
		double result = 0;
		panel.setTestType(ClassifierPanel.SUPPLIED_TEST_SET);
		
		Instances i = new Instances(new BufferedReader(new FileReader(file)));
		panel.setInstances(i);
		
		Instances testInst = new Instances(new BufferedReader(new FileReader(testFile)));
		panel.setTestInstance(testInst);
		
		panel.startClassifier();
		panel.m_RunThread.join();
		
		result = panel.evaluate;	
		
		panel.m_OutText.setText("");
		i.delete();
		i = null;				
		panel.m_RunThread = null;
		
		return result;
	}	
	
}
